<template>
	<view>
		<top-navigation>
			<template #left-slot>
				登录
			</template>
		</top-navigation>
		<view class="logo-box">
			<image class="logo" src="/static/images/logo.png"></image>
		</view>
		<view class="tips">{{loginProcess == 2?'为了更好的为您提供服务,我们将获取您的手机号码':'当前账户未登录，请登录后进行相关操作'}}</view>
		<button class="u-reset-button" v-if="loginProcess == 1" @tap="getUser">
			<view class="login-button">立 即 登 录</view>
		</button>
		<button class="u-reset-button" v-if="loginProcess == 2" open-type="getPhoneNumber" @getphonenumber="login">
			<view class="login-button">立 即 授 权</view>
		</button>
		<view class="login-button" v-if="loginProcess == 3" @tap="toIndex">立 即 登 录</view>

		<view class="user-agreement">
			<radio value="r1" :checked="isagree" @tap="isagree = !isagree" style="transform:scale(0.6)" />
			我已阅读并同意
			<text @tap="$toTreaty('用户协议')">《用户协议》</text>
			和
			<text @tap="$toTreaty('隐私政策')">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isagree: false, //是否选择阅读协议与政策
				userTemp: {}, //储存微信用户信息
				loginData: {}, //储存登录信息
				loginProcess: 2, // 1 获取用户信息 2 获取用户手机号 3登录
				lock: false //加锁，避免重复获取微信信息
			};
		},
		methods: {
			// 获取微信用户信息及登录code
			async getUser() {
				if (this.lock) return;
				this.lock = true;
				// 通过微信获取用户信息
				await wx.getUserProfile({
					lang: 'zh_CN',
					desc: '用于用户登录'
				}).then(res => {
					console.log(res,'用户信息');
					this.userTemp = res.userInfo;
				})
				await wx.login().then(res => {
					console.log(res,'登录信息');
					this.loginData = res;
				})
				this.loginProcess = 2;
				this.lock = false;
			},
			// 获取手机号信息
			async login(e) {
				console.log(e,'手机号信息');
				if (this.lock) return;
				this.lock = true;
				let params = {
					userCode: this.loginData.code,
					nickname: this.userTemp.nickName,
					avatar: this.userTemp.avatarUrl,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv
				};
			},
			// 直接登录
			toIndex() {
				if (this.lock) return;
				this.lock = true;
				if (!this.isagree) {
					uni.$showModal({
						title: '提示',
						content: '请您确认已完成阅读用户协议和隐私政策',
						showCancel: false
					});
					this.lock = false;
					return;
				}
				this.$store.dispatch('getUserInfo').then(() => {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				});
				this.lock = false;
			}
		}
	};
</script>

<style lang="scss">
	.logo-box {
		height: 500rpx;
		width: 750rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		.logo {
			width: 220rpx;
			height: 220rpx;
			margin-top: 150rpx;
		}
	}

	.tips {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 40rpx;
		font-size: 28rpx;
		line-height: 40rpx;
		color: #666666;
	}

	.login-button {
		width: 520rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		background: $uni-color-primary;
		font-size: 30rpx;
		color: #ffffff;
		border-radius: 12rpx;
		margin: 120rpx auto 0rpx auto;
		font-weight: bold;
	}

	.user-agreement {
		position: absolute;
		bottom: 50rpx;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 24rpx;

		text {
			color: $uni-color-primary;
		}
	}
</style>
